
'use strict';


/**
	Part of Zombie Navigator
	Copyright © 2015  Zombie Navigator Developers
	
	@file
	@module
	@license AGPL-3.0+
*/


/*
	Public API
	NOTE: This is a pseudo-module used by a Zombie script, which runs
	unprivileged. It should be stable as well as secure.
*/


/*
	Used modules
*/
const _env = require('../../platform/env');
const _meta = require('../../platform/meta');
const _notification = require('../../platform/notification');
const _io = require('../../platform/io');
const _console = require('../../platform/console');


/**
	Zombie scripts are only allowed to access variables starting with this
	string (*in addition* to the addon's prefix) thus resulting in:
	[addon's prefix] [this prefix] [script-controlled string]
	@private
*/
const ENV_PREFIX = 'DATA_';


/* Zombie API */

/**
	Get the module object for use in a Zombie script
	@public
*/
const getZombieAPI = scriptURI => {
	return {
		__proto__: null
		,
		zombie: Object.freeze({
			__proto__: null
			,
			productName: _meta.getProductName() + ''
			,
			version: _meta.getProductVersion() + ''
			,
			applicationName: _meta.getPlatformName() + ''
			,
			applicationVersion: _meta.getPlatformVersion() + ''
			,
			showNotification (text) {
				const modNotification = _notification;
				
				return !!modNotification.showNotification(text + '');
			}
			,
			print (...dataList) {
				const modIo = _io;
				
				let length = 0;
				if ('function' == typeof dump) {
					for (let data of dataList) {
						data = '' + data;
						length += modIo.print(data);
					}
				}
				return length | 0;
			}
		})
		,
		/**
			Console API
			@public
		*/
		console: _console.getConsole()
		,
		/**
			Environment variables
			@public
		*/
		env: new Proxy(Object.create(null), {
			get (o, name) {
				const env = _env;
				return env.get(ENV_PREFIX + name) + '';
			}
			,
			has (o, name) {
				const env = _env;
				return !!env.has(ENV_PREFIX + name);
			}
		})
		,
		/**
			Get the real name of an environment variable
			@public
			@method
			@param {string} name
			@returns {string}
		*/
		getEnvName (name) {
			const env = _env;
			return env.toEnvName(ENV_PREFIX + name) + '';
		}
	};
};


/*
	Exports
*/
exports.getZombieAPI = getZombieAPI;


// vim: ts=4 noet ai

